#include <iostream>

#include<string>
#include<memory>
#include <jsoncpp/json/json.h>

int main()
{
    Json::Value stu;

    stu["name"]="xiao wang";
    stu["id"]="123456";
    stu["score"].append(77);
    stu["score"].append(88.5);
    stu["score"].append(90);

    Json::StreamWriterBuilder swb;

    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());

    std::stringstream ss;

    int ret=sw->write(stu,&ss);

    if(ret!=0)
    {
        std::cout<<"Serialize failed!\n";
        return -1;
    }

    std::cout<<"序列化结果:\n"<<ss.str()<<std::endl;

    std::string str=ss.str();

    Json::Value root;

    Json::CharReaderBuilder crb;

    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

    bool ret1=cr->parse(str.c_str(),str.c_str()+str.size(),&root,nullptr);

    if(ret1==false)
    {
        std::cout<<" UnSerialize failed!\n";
        return -2;
    }
    
    std::cout<<"序列化结果：\n";
    std::cout<<"name: "<<root["name"].asCString()<<std::endl;
    std::cout<<"id: "<<root["id"].asCString()<<std::endl;
    std::cout<<"score: "<<root["score"][0].asDouble()<<std::endl;
    std::cout<<"score: "<<root["score"][1].asDouble()<<std::endl;
    std::cout<<"score: "<<root["score"][2].asDouble()<<std::endl;


    return 0;
}

